package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController     //@Response + @Controller
public class DishController {

    @Autowired
    private DishService dishService;

    /**
     *  菜品查询
     *  分页查询 + 模糊查询
     *  Request URL: http://localhost:8080/dish/page?page=1&pageSize=10
     *  Request Method: GET
     */
    @GetMapping("/dish/page")
    public ResultInfo findByPage(@RequestParam(name = "page",defaultValue = "1") Integer pageNum,
                                 @RequestParam(name = "pageSize",defaultValue = "10") Integer pageSize,
                                 String name){  //接收在请求行中的分页参数和模糊查询参数的name
        //1.调用service层
        Page<Dish> page = dishService.findByPage(pageNum,pageSize,name);

        //2. 返回响应
        return ResultInfo.success(page);
    }


    /**
     * 回显菜品分类信息
     * Request URL: http://localhost:8080/category/list?type=1
     * Request Method: GET
     */
    @GetMapping("/category/list")
    public ResultInfo findByType(Integer type){     //接收请求行中的参数
        //1. 调用service层
        List<Category> list = dishService.findByType(type);
        //2. 返回响应
        return ResultInfo.success(list);
    }

    /**
     * 菜品添加
     * Request URL: http://localhost:8080/dish
     * Request Method: POST
     */
    @PostMapping("/dish")
    public ResultInfo addDish(@RequestBody Dish dish){      //接收在请求体的参数
        //1. 调用service层
        dishService.addDish(dish);
        //2. 返回响应
        return ResultInfo.success();
    }


    /**
     * 回显菜品
     * Request URL: http://localhost:8080/dish/1413342036832100354
     * Request Method: GET
     */
    @GetMapping("/dish/{id}")
    public ResultInfo findById(@PathVariable Long id){  //接收restful风格的以路径形式存储的参数
        //1, 调用service层
        Dish dish = dishService.findById(id);
        //2. 返回响应
        return ResultInfo.success(dish);
    }

    /**
     * 修改菜品
     * Request URL: http://localhost:8080/dish
     * Request Method: PUT
     */
    @PutMapping("/dish")
    public ResultInfo updateById(@RequestBody Dish dish){       //接收请求体中的菜品信息
        //1. 调用service层
        dishService.updateById(dish);
        //2. 返回响应
        return ResultInfo.success();
    }




    /**
     * (批量)禁用/起售
     * Request URL: http://localhost:8080/dish/status/0?ids=1397851099502260226
     * Request Method: POST
     */
    @PostMapping("/dish/status/{status}")
    public ResultInfo updateStatus(@PathVariable Integer status,
                                   @RequestParam List<Long> ids){   //接收Restful风格和普通参数
        //1. 调用service层
        dishService.updateStatus(status,ids);
        //2. 返回响应
        return ResultInfo.success();
    }

    /**
     * (批量)删除
     * Request URL: http://localhost:8080/dish?ids=1397849739276890114,
     * Request Method: DELETE
     */
    @DeleteMapping("/dish")
    public ResultInfo deleteByIds(@RequestParam List<Long> ids){    //接收在路径后的参数
        //1.调用service层
        dishService.deleteByIds(ids);
        //2.返回响应
        return ResultInfo.success();
    }
}
